package com.wzdigit.wms.wms.controller.basic;

import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.framework.model.response.ErrorEntity;
import com.wzdigit.wms.common.utils.ValidatorUtil;
import com.wzdigit.wms.basic.client.PagerInfo;
import com.wzdigit.wms.basic.client.request.basic.CreateZoneGroupRequest;
import com.wzdigit.wms.basic.client.request.basic.SearchZoneGroupRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateStatusZoneGroupRequest;
import com.wzdigit.wms.basic.client.request.basic.UpdateZoneGroupRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.ZoneGroupApiResponse;
import com.wzdigit.wms.basic.client.response.basic.ZoneGroupResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.basic.ZoneGroupService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 区域 前端控制器
 * @author yangdongjie
 * @ZoneGroupController.java
 * @2020年12月31日下午5:56:18
 */
@RestController
@Api(tags = "区域")
@RequestMapping(value = WmsWareConstant.API_VERSION)
public class ZoneGroupController {

    @Autowired
    private ZoneGroupService zoneGroupService;

    private static final String REQUEST_MODEL = "/zonegroup";

    
    /**
     * 区域（下拉框）列表（启用状态）
     * @param <T>
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
	@ApiOperation(value = "区域（下拉框）列表（启用状态）",notes = "区域（下拉框）列表（启用状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/list")
    public List<ZoneGroupApiResponse> list(
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return zoneGroupService.list();
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }

    /**
     * 级联查询区域（下拉框）列表（所有状态）
     * @param <T>
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "级联查询区域（下拉框）列表（所有状态）",notes = "级联查询区域（下拉框）列表（所有状态）")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/listAllByWhsId")
    public List<ZoneGroupApiResponse> listAllByWhsId(
            @ApiParam(value = "仓库ID", required = false)
            @RequestParam(required = false) Integer whsId,
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) {
        try {
            return zoneGroupService.listAllByWhsId(whsId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
	
    /**
     * 查询区域列表
     * @param dto
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @SuppressWarnings("rawtypes")
	@ApiOperation(value = "查询区域列表",notes = "查询区域列表")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/searchZoneGroup")
    public PagerInfo<List<ZoneGroupResponse>> searchZoneGroup(
    		SearchZoneGroupRequest dto, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return zoneGroupService.searchZoneGroup(dto);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 查询区域
     * @param zoneGroupId
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "查询区域",notes = "查询区域")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @GetMapping(value = REQUEST_MODEL + "/getZoneGroup")
    public ZoneGroupResponse getZoneGroup(
    		@ApiParam(value = "区域ID", required = true) 
    		@RequestParam Integer zoneGroupId, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
            return zoneGroupService.getZoneGroup(zoneGroupId);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    
    /**
     * 创建区域
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "创建区域",notes = "创建区域")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PostMapping(value = REQUEST_MODEL + "/createZoneGroup")
    public ZoneGroupResponse createZoneGroup(
    		@RequestBody @Valid CreateZoneGroupRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return zoneGroupService.createZoneGroup(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改区域
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改区域",notes = "修改区域")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateZoneGroup")
    public ZoneGroupResponse updateZoneGroup(
    		@RequestBody @Valid UpdateZoneGroupRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return zoneGroupService.updateZoneGroup(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
    
    /**
     * 修改区域状态
     * @param dto
     * @param bindingResult
     * @param servletRequest
     * @param servletResponse
     * @return
     */
    @ApiOperation(value = "修改区域状态",notes = "修改区域状态")
    @ApiResponses({ @ApiResponse(code = 500, message = "系统出现异常", response = ErrorEntity.class) })
    @PutMapping(value = REQUEST_MODEL + "/updateStatus")
    public <T> Result<T> updateStatus(
    		@RequestBody @Valid UpdateStatusZoneGroupRequest dto, 
    		BindingResult bindingResult, 
    		HttpServletRequest servletRequest, 
            HttpServletResponse servletResponse) {
        try {
        	ValidatorUtil.validator(bindingResult);
            return zoneGroupService.updateStatus(dto, servletRequest);
        } catch (RestStatusException e) {
            e.printStackTrace();
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw e;
        }
    }
}
